
ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "Sound Splitter"
ENT.Author = "[GU]|ZLC|thomasfn"
ENT.Contact = "Don't."
ENT.Instructions = "Spawn it."
ENT.Purpose = ""

ENT.Spawnable = false
ENT.AdminSpawnable = false

ENT.ItemModel = "models/GraffitiRTST/creative_volume.mdl"
util.PrecacheModel( ENT.ItemModel )

ENT.IsSpeaker = true
ENT.IsSplitter = true

if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	
	ENT.NextUse = CurTime()
	ENT.PrintTimer = 7

	function ENT:Initialize()
		self.Entity:SetModel(self.ItemModel)
		self.Entity:PhysicsInit(SOLID_VPHYSICS)
		self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
		self.Entity:SetSolid(SOLID_VPHYSICS)
		
		if (!self.Data) then self.Data = {} end
		if (!self.Data[self:EntIndex()]) then self.Data[self:EntIndex()] = {} end
		
		self.Data[self:EntIndex()].Speakers = {}

		local phys = self.Entity:GetPhysicsObject()
		if (phys && phys:IsValid()) then phys:Wake() end
	end
	
	function ENT:Use( activator, ply )
		if (CurTime() < self.NextUse) then return end
		self.NextUse = CurTime()+1
	end
	
	function ENT:Think()
		for k, v in pairs( self.Data[ self:EntIndex() ].Speakers ) do
			if ((!v) || (!v:IsValid()) || (!v.IsSpeaker)) then
				table.remove( self.Data[ self:EntIndex() ].Speakers, k )
				return
			end
		end
	end
	
	function ENT:Sound_Play( soundname )
		for _, v in pairs( self.Data[ self:EntIndex() ].Speakers ) do
			if ((v) && (v:IsValid()) && (v.IsSpeaker)) then
				v:Sound_Play( soundname )
			end
		end
	end
	
	function ENT:Sound_Stop()
		for _, v in pairs( self.Data[ self:EntIndex() ].Speakers ) do
			if ((v) && (v:IsValid()) && (v.IsSpeaker)) then
				v:Sound_Stop()
			end
		end
	end
	
	function ENT:Sound_Get()
		for _, v in pairs( self.Data[ self:EntIndex() ].Speakers ) do
			if ((v) && (v:IsValid()) && (v.IsSpeaker)) then
				return v:Sound_Get()
			end
		end
		return "Unknown"
	end
	
end

if (CLIENT) then

	function ENT:Draw()
		// Draw our entity
		self.BaseClass.Draw( self )

	end
	
end